Page({
    data: {
      longitude: 0,
      latitude: 0,
      markers: [],
      polyline: [],
      selectedStore: null,
      stores: [],
      currentMarkerId: null,
      currentLocation: null, // 新增当前定位数据
       clickedLocation: null // 新增点击位置数据
    },
  
    onLoad() {
      this.getLocation();
    },
  
    getLocation() {
      wx.getLocation({
        type: 'gcj02',
        success: (res) => {
          console.log('当前位置信息：', {
            经度: res.longitude,
            纬度: res.latitude,
            速度: res.speed,
            精确度: res.accuracy
          });
          
          this.setData({
            longitude: res.longitude,
            latitude: res.latitude,
            currentLocation: { // 保存当前定位数据
              longitude: res.longitude,
              latitude: res.latitude
            }
          }, () => {
            this.initMarkers(res.longitude, res.latitude);
          });
        },
        fail: (err) => {
          console.error('获取位置失败', err);
          wx.showToast({
            title: '请开启位置权限',
            icon: 'none'
          });
        }
      });
    },
  
    initMarkers(userLongitude, userLatitude) {
      const stores = [
        {
          id: 1,
          name: '半口奶油蛋糕店（团结店）',
          address: '长沙市天心区团结路123号',
          longitude: 112.9926,
          latitude: 28.1940
        },
        {
          id: 2,
          name: '半口奶油蛋糕店（黄兴广场店）',
          address: '长沙市天心区黄兴路步行街45号',
          longitude: 112.9958,
          latitude: 28.1876
        }
      ];
      
      const currentLocationMarker = {
        id: 0,
        longitude: userLongitude,
        latitude: userLatitude,
        title: '当前位置',
        iconPath: '../../images/about/地址.png',
        width: 40,
        height: 40,
        zIndex: 10,
        callout: { // 添加点击后的气泡
          content: '当前位置\n经度: ' + userLongitude + '\n纬度: ' + userLatitude,
          color: '#000',
          fontSize: 14,
          borderRadius: 10,
          bgColor: '#fff',
          padding: 10,
          display: 'ALWAYS' // 总是显示
        }
      };
      
      const storeMarkers = stores.map(store => ({
        id: store.id,
        longitude: store.longitude,
        latitude: store.latitude,
        title: store.name,
        iconPath: '../../images/about/地址 (1).png',
        width: 40,
        height: 40,
        zIndex: 5,
        callout: { // 添加店铺的气泡
          content: store.name + '\n经度: ' + store.longitude + '\n纬度: ' + store.latitude,
          color: '#000',
          fontSize: 14,
          borderRadius: 10,
          bgColor: '#fff',
          padding: 10,
          display: 'BYCLICK' // 点击时显示
        }
      }));
      
      this.setData({
        markers: [currentLocationMarker, ...storeMarkers],
        stores
      });
    },
  
    onMapTap(e) {
        console.log('点击地图位置：', {
          经度: e.detail.longitude,
          纬度: e.detail.latitude
        });
        
        this.setData({
          selectedStore: null,
          currentMarkerId: null,
          clickedLocation: { // 保存点击位置
            longitude: e.detail.longitude,
            latitude: e.detail.latitude
          }
        });
      },
  
    onMarkerTap(e) {
      const markerId = e.markerId;
      console.log('点击标记点ID:', markerId);
      
      if (markerId === 0) {
        // 点击了当前位置标记
        wx.showToast({
          title: '这是您当前的位置',
          icon: 'none'
        });
        return;
      }
      
      const selectedStore = this.data.stores.find(store => store.id === markerId);
      
      if (selectedStore) {
        console.log('所选店铺经纬度：', {
          经度: selectedStore.longitude,
          纬度: selectedStore.latitude
        });
        
        this.setData({
          selectedStore,
          currentMarkerId: markerId
        });
      }
    },
  
    navigateToStore() {
      const { selectedStore } = this.data;
      if (!selectedStore) return;
      
      wx.openLocation({
        latitude: selectedStore.latitude,
        longitude: selectedStore.longitude,
        name: selectedStore.name,
        address: selectedStore.address,
        scale: 18
      });
    }
  });